<div class="bjui-pageContent">
	@if($method=='add')
	<form action="{{url('admin/group/groupAdd')}}" data-toggle="validate" method="post" data-close-current="true">
    	{{csrf_field()}}
        <input type="hidden" name="roles" id="ztreepid" value="">
        <div class="bjui-row col-1">
            <label class="row-label">角色名:</label>
            <div class="row-input required">
            	<input type="text" id="name" name="name" value="" data-rule="required;">
            </div>
            <label class="row-label">规则:</label>
            <div class="row-input">
                <input type="text" id="j_ztree_menus" data-toggle="selectztree" readonly="readonly" size="30" data-tree="#j_select_tree" value="" onclick="S_Click('j_select_tree')">
                <ul id="j_select_tree" class="ztree hide" data-toggle="ztree" data-expand-all="true" data-check-enable="true" data-on-check="S_NodeCheck" style="max-height: 500px;">
                    @foreach($auth as $v)
						<li data-id="{{$v->id}}" data-role="{{$v->role}}" data-pid="{{$v->a_id}}"> {{$v->name}}</li>
                  	@endforeach
                </ul>
            </div>
        </div>
    </form>
	@elseif($method=='edit')
    <form action="{{url('admin/group/groupEdit')}}" data-toggle="validate" method="post" data-close-current="true">
    	{{csrf_field()}}
        <input type="hidden" name="id" value="{{$group->id}}">
        <input type="hidden" name="roles" id="ztreepid" value="{{$roles}}">
        <div class="bjui-row col-1">
            <label class="row-label">角色名:</label>
            <div class="row-input required">
            	<input type="text" id="name" name="name" value="{{$group->name}}" data-rule="required;">
            </div>
            <label class="row-label">规则:</label>
            <div class="row-input">
                <input type="text" id="j_ztree_menus" data-toggle="selectztree" readonly="readonly" size="30" data-tree="#j_select_tree" value="{{$names}}" onclick="S_Click('j_select_tree')">
                <ul id="j_select_tree" class="ztree hide" data-toggle="ztree" data-expand-all="true" data-check-enable="true" data-on-check="S_NodeCheck" style="max-height: 500px;">
                    @foreach($auth as $v)
						<li data-id="{{$v->id}}" data-role="{{$v->role}}" data-pid="{{$v->a_id}}"> {{$v->name}}</li>
                  	@endforeach
                </ul>
            </div>
        </div>
    </form>
    @endif
    
</div>
<div class="bjui-pageFooter">
    <ul>
        <li><button type="button" class="btn-close" data-icon="close">关闭</button></li>
        <li><button type="submit" class="btn-default" data-icon="check">保存</button></li>
    </ul>
</div>

<script type="text/javascript">

	// 初始化选择节点
	function S_Click(treeId){
		@if(isset($ids))
		var zTree = $.fn.zTree.getZTreeObj(treeId);
		var ids = {{$ids}};
		for(var i=0;i<ids.length;i++){
			zTree.checkNode(zTree.getNodeByParam("id",ids[i]),true,false);
		}
		@endif
	}

	//选择z-tree事件
	function S_NodeCheck(e, treeId, treeNode) {
	    var zTree = $.fn.zTree.getZTreeObj(treeId),
	        nodes = zTree.getCheckedNodes(true)
	    var ids = '', roles = '', names = ''
	    
	    for (var i = 0; i < nodes.length; i++) {
	        ids   += ','+ nodes[i].id
	        roles += ','+ nodes[i].role
	        names += ','+ nodes[i].name
	    }
	    if (ids.length > 0) {
	        ids = ids.substr(1), roles = roles.substr(1), names = names.substr(1)
	    }

	    var $from = $('#'+ treeId).data('fromObj')
	    
	    if ($from && $from.length) {
	    	$from.val(names);
	    	$("#ztreepid").val(roles); 
	    }
	}
	
</script>
